<?php

namespace App\Admin\Renders;

use App\Factories\PosterFactory;
use App\Models\Season;
use Dcat\Admin\Support\LazyRenderable;
use Dcat\Admin\Widgets\Alert;
use Exception;
use Throwable;

class PreviewPosterRender extends LazyRenderable
{
    /**
     * @throws Throwable
     */
    public function render(): string
    {
        $season = Season::find($this->payload['season_id']);
        $player = $season->players()->latest()->first();

        if ($player) {
            try {
                $poster = new PosterFactory($player, true);

                return $poster->show();
            } catch (Exception $e) {
                $alert = Alert::make($e->getMessage(), '错误提醒');
                $alert->danger();

                return $alert->render();
            }
        } else {
            return '<h1>当前活动没有选手，请确定选手头像后再预览</h1>';
        }
    }
}
